package org.study.hadoop.md.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.study.hadoop.md.entity.Comment;
import org.study.hadoop.md.entity.Post;
import org.study.hadoop.md.entity.User;

/**
 * 生成StackOverFlow模拟数据
 */
public class MockUtil {
	
	private static Random random = new Random();
	private static ClassLoader loader = Thread.currentThread().getContextClassLoader();

	private static String[] wikiArray = new String[] {
		"http://en.wikipedia.org/Java",
		"http://en.wikipedia.org/Php",
		"http://en.wikipedia.org/Spark",
		"http://en.wikipedia.org/Hive",
		"http://en.wikipedia.org/Hadoop",
		"http://en.wikipedia.org/Maven",
		"http://en.wikipedia.org/Scala"
	};
	
	public static void main(String[] args) throws IOException {
		createUserFile();
//		createCommentFile();
//		createPostFile();
//		createFile();
	}
	
	private static void createPostFile() throws IOException {
		InputStream userIn = loader.getResourceAsStream("user.conf");
		InputStream postIn = loader.getResourceAsStream("post.conf");
		InputStream commentIn = loader.getResourceAsStream("comment.conf");
		InputStream bodyIn = loader.getResourceAsStream("body.conf");
		InputStream tagIn = loader.getResourceAsStream("tag.conf");
		InputStream titleIn = loader.getResourceAsStream("title.conf");
		BufferedReader userReader = new BufferedReader(new InputStreamReader(userIn));
		BufferedReader postReader = new BufferedReader(new InputStreamReader(postIn));
		BufferedReader commentReader = new BufferedReader(new InputStreamReader(commentIn));
		BufferedReader bodyReader = new BufferedReader(new InputStreamReader(bodyIn));
		BufferedReader tagReader = new BufferedReader(new InputStreamReader(tagIn));
		BufferedReader titleReader = new BufferedReader(new InputStreamReader(titleIn));
		List<String> userList = new ArrayList<String>();
		List<String> postList = new ArrayList<String>();
		List<String> commentList = new ArrayList<String>();
		List<String> bodyList = new ArrayList<String>();
		List<String> tagList = new ArrayList<String>();
		List<String> titleList = new ArrayList<String>();
		String line = null;
		while ((line = userReader.readLine()) != null) {
			line = line.trim();
			if (line.equals("")) {
				continue;
			}
			userList.add(line);
		}
		line = null;
		while ((line = postReader.readLine()) != null) {
			line = line.trim();
			if (line.equals("")) {
				continue;
			}
			postList.add(line);
		}
		line = null;
		while ((line = commentReader.readLine()) != null) {
			line = line.trim();
			if (line.equals("")) {
				continue;
			}
			commentList.add(line);
		}
		line = null;
		while ((line = bodyReader.readLine()) != null) {
			line = line.trim();
			if (line.equals("")) {
				continue;
			}
			bodyList.add(line);
		}
		line = null;
		while ((line = tagReader.readLine()) != null) {
			line = line.trim();
			if (line.equals("")) {
				continue;
			}
			tagList.add(line);
		}
		line = null;
		while ((line = titleReader.readLine()) != null) {
			line = line.trim();
			if (line.equals("")) {
				continue;
			}
			titleList.add(line);
		}
		userReader.close();
		postReader.close();
		commentReader.close();
		bodyReader.close();
		tagReader.close();
		titleReader.close();
		Post post;
		String id;						
		int postTypeId;				
		String parentId;				
		String acceptedAnswerId;		
		Date creationDate;			
		Date lastActivityDate;		
		int score;						
		int viewCount;					
		String body;					
		String ownerUserId;				
		int commentCount;				
		String title;					
		String tags;					
		int answerCount;	
		File file = new File("post.txt");
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		for (int i = 0; i < 500; i++) {
			id = postList.get(random.nextInt(postList.size()));
			postTypeId = random.nextInt(2) + 1;
			creationDate = DateUtil.randomDate("2010-01-01", "2012-01-01");
			lastActivityDate = DateUtil.randomDate("2011-01-01", "2017-01-01");
			score = random.nextInt(99) + 1;
			viewCount = random.nextInt(99) + 1;
			ownerUserId = userList.get(random.nextInt(userList.size()));
			body = bodyList.get(random.nextInt(bodyList.size()));
			if (postTypeId == 1) {
				acceptedAnswerId = commentList.get(random.nextInt(commentList.size()));
				title = titleList.get(random.nextInt(titleList.size()));
				tags = tagList.get(random.nextInt(tagList.size()));
				answerCount = random.nextInt(9) + 1;
				post = new Post(id, postTypeId, DateUtil.formatDateAll(creationDate), DateUtil.formatDateAll(lastActivityDate), score, viewCount, ownerUserId, body, acceptedAnswerId, title, tags, answerCount);
			} else {
				parentId = commentList.get(random.nextInt(commentList.size()));
				commentCount = random.nextInt(9) + 1;
				body = wikiArray[random.nextInt(wikiArray.length)];
				post = new Post(id, postTypeId, DateUtil.formatDateAll(creationDate), DateUtil.formatDateAll(lastActivityDate), score, viewCount, ownerUserId, body, parentId, commentCount);
			}
			fileOutputStream.write(post.toString().getBytes());
			fileOutputStream.write("\n".getBytes());
		}
		fileOutputStream.close();
	}
	
	private static void createCommentFile() throws IOException {
		InputStream userIn = loader.getResourceAsStream("user.conf");
		InputStream postIn = loader.getResourceAsStream("post.conf");
		InputStream commentIn = loader.getResourceAsStream("comment.conf");
		InputStream bodyIn = loader.getResourceAsStream("body.conf");
		BufferedReader userReader = new BufferedReader(new InputStreamReader(userIn));
		BufferedReader postReader = new BufferedReader(new InputStreamReader(postIn));
		BufferedReader commentReader = new BufferedReader(new InputStreamReader(commentIn));
		BufferedReader bodyReader = new BufferedReader(new InputStreamReader(bodyIn));
		List<String> userList = new ArrayList<String>();
		List<String> postList = new ArrayList<String>();
		List<String> commentList = new ArrayList<String>();
		List<String> bodyList = new ArrayList<String>();
		String line = null;
		while ((line = userReader.readLine()) != null) {
			line = line.trim();
			if (line.equals("")) {
				continue;
			}
			userList.add(line);
		}
		line = null;
		while ((line = postReader.readLine()) != null) {
			line = line.trim();
			if (line.equals("")) {
				continue;
			}
			postList.add(line);
		}
		line = null;
		while ((line = commentReader.readLine()) != null) {
			line = line.trim();
			if (line.equals("")) {
				continue;
			}
			commentList.add(line);
		}
		line = null;
		while ((line = bodyReader.readLine()) != null) {
			line = line.trim();
			if (line.equals("")) {
				continue;
			}
			bodyList.add(line);
		}
		userReader.close();
		postReader.close();
		commentReader.close();
		bodyReader.close();
		Comment comment;
		String id;
		String postId;
		String text;
		Date creationDate;
		String userId;
		File file = new File("comment.txt");
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		for (int i = 0; i < 1000; i++) {
			id = commentList.get(random.nextInt(commentList.size()));
			postId = postList.get(random.nextInt(postList.size()));
			text = bodyList.get(random.nextInt(bodyList.size()));
			creationDate = DateUtil.randomDate("2010-01-01", "2012-01-01");
			userId = userList.get(random.nextInt(userList.size()));
			comment = new Comment(id, postId, userId, text, DateUtil.formatDateAll(creationDate));
			fileOutputStream.write(comment.toString().getBytes());
			fileOutputStream.write("\n".getBytes());
		}
		fileOutputStream.close();
	}
	
	private static void createUserFile() throws IOException {
		InputStream userIn = loader.getResourceAsStream("user.conf");
		InputStream stateIn = loader.getResourceAsStream("state.conf");
		InputStream displayNameIn = loader.getResourceAsStream("displayname.conf");
		BufferedReader userReader = new BufferedReader(new InputStreamReader(userIn));
		BufferedReader stateReader = new BufferedReader(new InputStreamReader(stateIn));
		BufferedReader displayNameReader = new BufferedReader(new InputStreamReader(displayNameIn));
		String line = null;
		List<String> userList = new ArrayList<String>();
		List<String> stateList = new ArrayList<String>();
		List<String> displayNameList = new ArrayList<String>();
		while ((line = userReader.readLine()) != null) {
			line = line.trim();
			if (line.equals("")) {
				continue;
			}
			userList.add(line);
		}
		line = null;
		while ((line = stateReader.readLine()) != null) {
			line = line.trim();
			if (line.equals("")) {
				continue;
			}
			stateList.add(line);
		}
		line = null;
		while ((line = displayNameReader.readLine()) != null) {
			line = line.trim();
			if (line.equals("")) {
				continue;
			}
			displayNameList.add(line);
		}
		userReader.close();
		stateReader.close();
		displayNameReader.close();
		User user;
		int id;
		int reputation;
		Date creationDate;
		String displayName;
		String emailHash;
		Date lastAccessDate;
		String location;
		int age;
		int views;
		int upVotes;
		int downVotes;
		File file = new File("users.txt");
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		for (int i = 0; i < 10000; i++) {
			id = Integer.parseInt(userList.get(random.nextInt(userList.size())));
			reputation = random.nextInt(9999) % (9999 - 1000 + 1) + 1000;
			creationDate = DateUtil.randomDate("2009-01-01", "2011-01-01");
			displayName = displayNameList.get(random.nextInt(displayNameList.size()));
			emailHash = MD5Encrypt.MD5Encode(displayName);
			lastAccessDate = DateUtil.randomDate("2011-01-01", "2017-01-01");
			location = stateList.get(random.nextInt(stateList.size()));
			age = random.nextInt(80) % (80 - 16 + 1) + 16;
			views = random.nextInt(99) % (99 - 10 + 1) + 10;
			upVotes = random.nextInt(999) % (999 - 100 + 1) + 100;
			downVotes = random.nextInt(99) % (99 - 10 + 1) + 10;
			user = new User(id, reputation, DateUtil.formatDateAll(creationDate), DateUtil.formatDateAll(lastAccessDate), displayName, emailHash, location, age, views, upVotes, downVotes);
			fileOutputStream.write(user.toString().getBytes());
			fileOutputStream.write("\n".getBytes());
		}
		fileOutputStream.close();
	}
	
	private static void createFile() throws IOException {
		File file = new File("ser.conf");
		FileOutputStream fos = new FileOutputStream(file);
		String id;
		for (int i = 0; i < 100; i++) {
			id = random.nextInt(77777) % (77777 - 77700 + 1) + 77700 + "";
			fos.write(id.getBytes());
			fos.write("\n".getBytes());
		}
		fos.close();
	}
	
//	
//	private static String[] statesArray = new String[] { "AL", "AK", "AZ", "AR",
//		"CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN",
//		"IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS",
//		"MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND",
//		"OH", "OK", "OR", "PA", "RI", "SC", "SF", "TN", "TX", "UT",
//		"VT", "VA", "WA", "WV", "WI", "WY" };
//	/**
//	 * 生成用户文件
//	 */
//	private static void createUserFile(String filePath) throws IOException {
//		User user;
//		int id;
//		int reputation;
//		Date creationDate;
//		String displayName;
//		String emailHash;
//		Date lastAccessDate;
//		String location;
//		int age;
//		int views;
//		int upVotes;
//		int downVotes;
//		File file = new File(filePath);
//		FileOutputStream fileOutputStream = new FileOutputStream(file);
//		for (int i = 0; i < 100; i++) {
//			id = random.nextInt(55555) % (55555 - 55500 + 1) + 55500;
//			reputation = random.nextInt(9999) % (9999 - 1000 + 1) + 1000;
//			creationDate = DateUtil.randomDate("2009-01-01", "2011-01-01");
//			displayName = StringUtil.getRandomName(random.nextInt(8) % (8 - 4 + 1) + 8);
//			emailHash = MD5Encrypt.MD5Encode(displayName);
//			lastAccessDate = DateUtil.randomDate("2011-01-01", "2017-01-01");
//			location = statesArray[random.nextInt(49) % (49 - 0 + 1) + 0];
//			age = random.nextInt(80) % (80 - 16 + 1) + 16;
//			views = random.nextInt(99) % (99 - 10 + 1) + 10;
//			upVotes = random.nextInt(999) % (999 - 100 + 1) + 100;
//			downVotes = random.nextInt(99) % (99 - 10 + 1) + 10;
//			user = new User(id, reputation, DateUtil.formatDateAll(creationDate), displayName, emailHash, DateUtil.formatDateAll(lastAccessDate), location, age, views, upVotes, downVotes);
//			fileOutputStream.write(user.toString().getBytes());
//			fileOutputStream.write("\n".getBytes());
//		}
//		fileOutputStream.close();
//	}
//	/**
//	 * 生成评论文件
//	 */
//	private static void createCommentFile(String filePath) throws IOException {
//		Comment comment;
//		int id;
//		int postId;
//		String text;
//		Date creationDate;
//		int userId;
//		File file = new File(filePath);
//		FileOutputStream fileOutputStream = new FileOutputStream(file);
//		for (int i = 0; i < 1000; i++) {
//			id = random.nextInt(9999999) % (9999999 - 1000000 + 1) + 1000000;
//			postId = random.nextInt(9999999) % (9999999 - 1000000 + 1) + 1000000;
//			text = StringUtil.getRandomString(random.nextInt(20) % (20 - 10 + 1) + 10);
//			creationDate = DateUtil.randomDate("2009-01-01", "2017-01-01");
//			userId = random.nextInt(55555) % (55555 - 55500 + 1) + 55500;
//			comment = new Comment(id, postId, text, DateUtil.formatDateAll(creationDate), userId);
//			fileOutputStream.write(comment.toString().getBytes());
//			fileOutputStream.write("\n".getBytes());
//		}
//		fileOutputStream.close();
//	}
//	public static void main(String[] args) throws IOException {
////		createCommentFile("E://comment.txt");
//		createUserFile("E://user.txt");
//	}
}
